package csu.rjxyg.action.userAction;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.bson.types.ObjectId;

import csu.rjxyg.action.BaseAction;
import csu.rjxyg.action.constant.UserConstant;
import csu.rjxyg.bean.core.BaseBean;
import csu.rjxyg.bean.core.BaseList;
import csu.rjxyg.bean.core.BaseObject;
import csu.rjxyg.dao.BaseDaoAdapter;
import csu.rjxyg.factory.core.DaoFactory;
import csu.rjxyg.factory.core.UtilFactory;
import csu.rjxyg.util.LogUtil;

public class UpdateUserInfoAction extends BaseAction {
	
	private LogUtil log;

	private Map tempUser; 
	
	public Map getTempUser() {
		return tempUser;
	}

	public void setTempUser(Map tempUser) {
		this.tempUser = tempUser;
	}

	public UpdateUserInfoAction(){
		this.log = UtilFactory.getLogUtil(this.getClass());
	}
	
	public void updateUserInfo() throws IOException{
		String name = null;
		BaseObject object = (BaseObject) this.getSession().get(UserConstant.USER_SESSION);
		name = (String) object.get(UserConstant.USER_ID);
		
		
		String oldUserId = null;
		Set set = tempUser.entrySet();        
		Iterator i = set.iterator();        
		while(i.hasNext()){     
		     Entry entry=(Entry) i.next();   
		     if( entry.getKey().equals(UserConstant.USER_ID) ){
		    	 oldUserId = (String) entry.getValue();
		    	 i = set.iterator();
		    	 break;
		     }
		}
		if( oldUserId == null ){
			return;
		}
		BaseDaoAdapter userDao = DaoFactory
				.getDaoByColName(UserConstant.COLLECTION_NAME);
		BaseObject oldObject = userDao.getBeanInfoById(oldUserId);
		if( oldObject == null ){
			return;
		}
		
		while(i.hasNext()){     
		     Entry entry=(Entry) i.next();
		     if( entry.getKey().equals(UserConstant.USER_PASS) ){
		    	 continue;
		     }else if( entry.getKey().equals("_id") ){
		    	 continue;
		     }else if( entry.getKey().equals(UserConstant.USER_XP) ){
		    	 if( oldObject.get(UserConstant.USER_XP) != null ){
		    		 if( oldObject.get(UserConstant.USER_XP).equals(entry.getValue()) ){
			    		 continue;
			    	 }
		    	 }
		    	
		    	 
		    	 String temppath = ServletActionContext.getServletContext().getRealPath("/");
		    	 temppath += entry.getValue();
		    	 
		    	
		    	 String realpath = ServletActionContext.getServletContext().getRealPath("/userImages");
			     File file = new File(temppath);
			     
			     if (file != null) {
			    	 String realFile = oldUserId;
			         File savefile = new File(new File(realpath), realFile);
			         if (!savefile.getParentFile().exists())
			        	 savefile.getParentFile().mkdirs();
			         FileUtils.copyFile(file, savefile);	
			         
			      }
			     oldObject.set((String) entry.getKey(), "userImages/" + oldUserId);
		    	 continue;
		     }
		     oldObject.set((String) entry.getKey(), entry.getValue());
		}
		//将权限转为系统识别
		oldObject = this.changePermissionName(oldObject);
		
		if( userDao.updateBean(oldUserId, oldObject)  == false){
			log.info(name + "调用updateBean方法出错");
			return;
		}
		
		//将权限转为中文
		oldObject = this.changePermissionName(oldObject);
		this.out((String) oldObject.getBean().toString());
		//将权限转为系统识别
		oldObject = this.changePermissionName(oldObject);
		if( name.equals(oldUserId) ){
			this.getSession().put(UserConstant.USER_SESSION, oldObject);
		}
		
		
		log.info(name + "修改了" + oldUserId + "的信息");
	}
}
